package com.example.bomc.controller;

import com.example.bomc.common.msg.CodeMsg;
import com.example.bomc.model.Trouble;
import com.example.bomc.service.fixStaffService.StaffService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

@Controller
public class StaffController {


    private static final Logger logger = LoggerFactory.getLogger(LoginController.class);


    @Autowired
    StaffService staffService;

    /**
     * 登录
     */
    @RequestMapping(path = {"/stafflogin/"}, method = {RequestMethod.GET})
    @ResponseBody
    public int stafflogin(@RequestParam("username") String username,
                     @RequestParam("password") String password) {
        try {
            CodeMsg msg = staffService.selectByFixName(username,password);
            if (msg.getRetCode() != 0) {
                return -1;
            } else {
                return 0;
            }

        } catch (Exception e) {
            logger.error("登录异常" + e.getMessage());
            return -1000;
        }
    }
    /**
     * 解决问题
     */

    @RequestMapping(path = {"/fixTrouble/"}, method = {RequestMethod.POST})
    @ResponseBody
    public CodeMsg fixTrouble(@RequestParam("id") int id,
                            @RequestParam("tag") String tag,
                            HttpServletResponse response) {

        CodeMsg msg = new CodeMsg();
        try{

            msg = staffService.fixTrouble(tag,id);
        }catch (Exception e){
            msg.setRetCode(-1);
            msg.setMessage("失败！！！");
            logger.error("失败！！！"+e);
            return msg;
        }
        return msg;
    }

    /**
     * 查询所有共性问题
     */
    @RequestMapping(path = {"/selAllTrouble/"}, method = {RequestMethod.POST})
    @ResponseBody
    public CodeMsg selAllTrouble(
                              HttpServletResponse response) {

        CodeMsg msg = new CodeMsg();
        try{
            msg = staffService.selAllTrouble();
        }catch (Exception e){
            msg.setRetCode(-1);
            msg.setMessage("失败！！！");
            logger.error("失败！！！"+e);
            return msg;
        }
        return msg;
    }


    /**
     * 查询共性问题
     */

    @RequestMapping(path = {"/selTroubleByTrouble/"}, method = {RequestMethod.POST})
    @ResponseBody
    public CodeMsg selTroubleByTrouble(
            @RequestParam("trouble") String trouble,
            HttpServletResponse response) {

        CodeMsg msg = new CodeMsg();
        try{
            msg = staffService.selTroubleByTrouble(trouble);
        }catch (Exception e){
            msg.setRetCode(-1);
            msg.setMessage("失败！！！");
            logger.error("失败！！！"+e);
            return msg;
        }
        return msg;
    }
    /**
     * 新增共性问题
     */
    @RequestMapping(path = {"/addSameTrouble/"}, method = {RequestMethod.POST})
    @ResponseBody
    public CodeMsg addSameTrouble(
            @RequestParam("trouble") String trouble,
            @RequestParam("trouble") String reply,
            @RequestParam("trouble") String fixwork,
            HttpServletResponse response) {

        CodeMsg msg = new CodeMsg();
        Trouble trouble1 = new Trouble();
        try{
            trouble1.setFixwork(fixwork);
            trouble1.setReply(reply);
            trouble1.setTrouble(trouble);
            msg = staffService.addSameTrouble(trouble1);
        }catch (Exception e){
            msg.setRetCode(-1);
            msg.setMessage("失败！！！");
            logger.error("失败！！！"+e);
            return msg;
        }
        return msg;
    }

    /**
     * 修改共性问题
     */

    @RequestMapping(path = {"/updateTrouble/"}, method = {RequestMethod.POST})
    @ResponseBody
    public CodeMsg updateTrouble(
            @RequestParam("id") int id,
            @RequestParam("trouble") String reply,
            HttpServletResponse response) {

        CodeMsg msg = new CodeMsg();
        try{
            msg = staffService.updateTrouble(reply,id);
        }catch (Exception e){
            msg.setRetCode(-1);
            msg.setMessage("失败！！！");
            logger.error("失败！！！"+e);
            return msg;
        }
        return msg;
    }
    /**
     * 删除共性问题
     */

    @RequestMapping(path = {"/deleteStaff/"}, method = {RequestMethod.POST})
    @ResponseBody
    public CodeMsg deleteStaff(
            @RequestParam("trouble") String fixname,
            HttpServletResponse response) {

        CodeMsg msg = new CodeMsg();
        try{
            msg = staffService.deleteStaff(fixname);
        }catch (Exception e){
            msg.setRetCode(-1);
            msg.setMessage("失败！！！");
            logger.error("失败！！！"+e);
            return msg;
        }
        return msg;
    }
}
